angular.module('Controller')

.controller('QgyFinanceStatisticCtrl', ($scope,NgTableParams,urls,menuManage,userData,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            apartmentId:$localStorage['accountStore']
            payTimeStart:new Date(moment().format('YYYY-MM')+'-01')
            payTimeEnd:new Date()
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.offLeaseStatusList=commonConstant.offLeaseStatusList(true)
    $scope.financeStatusList=commonConstant.financeStatusList(true)
    $scope.contractPayWayList=commonConstant.payWayList()
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            apartmentId: $scope.search.apartmentId,
            payTime:undefined
        }
        reqData.payTimeStart=moment($scope.search.payTimeStart).format('YYYY-MM-DD') if $scope.search.payTimeStart
        reqData.payTimeEnd=moment($scope.search.payTimeEnd).format('YYYY-MM-DD') if $scope.search.payTimeEnd
        $scope.currentSearchData=angular.copy($scope.search)
        $http.get('/finance',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
        $http.get('/finance/sum',params:reqData)
        .success((resp)->
            if resp.code==0
                $scope.sumInfo=resp.message
        )
    _.extend $scope,
        newData: ->
            data={type:1,enabled:1}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            showModal('edit',theData)
        viewData:(item)->
            return
            theData=angular.copy(item)
            showModal('view',theData)
        resetSearch: ->
            initSearchData()
        exportData:->
            $scope.exporting = true;
            data = $scope.currentSearchData;
            reqData = {
                page:0
                size:99999
                apartmentId: data.apartmentId,
                payTime: undefined
            }
            reqData.payTimeStart=moment(data.payTimeStart).format('YYYY-MM-DD') if data.payTimeStart
            reqData.payTimeEnd=moment(data.payTimeEnd).format('YYYY-MM-DD') if data.payTimeEnd
#            postParam = commonUtil.transferGetParam(reqData)
            $http.get('/finance/excel',params:reqData)
            .success((resp)->
                $scope.exporting = false
                if resp.code == 0
                    filename = resp.message
                    url=urls.backend+'/finance/download?file=' + filename
                    url=url+'&token='+userData.getToken()
                    console.log('url',url)
                    window.open(url, '_blank')
            )
            .error((err)->
                $scope.exporting = false
            )
        auditData:(item)->
            return
            theData=angular.copy(item)
            showAuditModal('audit',theData)
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        assignAppoint:(item)->
            showAssignModal('assign',angular.copy(item))
        cancelAppoint:(item)->
            showCancelModal(angular.copy(item))
        feedbackResult:(item)->
            showFeedbackModal('assign',angular.copy(item))

    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyAppointManageEditCtrl',
            templateUrl: 'templates/business-manage/appointment-manage/appointment-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
    showAuditModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyFinanceAuditModalCtrl',
            templateUrl: 'templates/finance-service/finance-audit/finance-audit-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)